home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Java / Java_File_Save.bsh < prev    next >
Text File  |  2013-07-28  |  5KB  |  145 lines

  1. /*
  2.  * Java_File_Save.bsh - a BeanShell macro for saving new java files.
  3.  *
  4.  * Copyright (C) 2004 Nicholas O'Leary nol@deferential.net
  5.  * 
  6.  * :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=true:
  7.  * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  8.  *
  9.  * {{{ License
  10.  * This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License
  12.  * as published by the Free Software Foundation; either version 2
  13.  * of the License, or any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with the jEdit program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  23.  * }}}
  24.  *
  25.  * Notes:
  26.  *  Only the first 250 lines of the buffer are scanned for a suitable
  27.  *  class or interface declaration.
  28.  *  
  29.  * Changes:
  30.  *  17-May-04: Only scans if the edit mode is either 'java' or the default mode
  31.  *           : Ignores declarations that are in multiline comments
  32.  *  08-Jun-04: If an infinite loop is hit (1000 iterations) in the comment
  33.  *           : parsing, it now opens the default save dialog, rather than
  34.  *           : just returning.
  35.  * $Id: Java_File_Save.bsh 21353 2012-03-14 09:46:51Z jojaba_67 $
  36.  */
  37.  
  38. //Localization
  39. final static String InfiniteLoopError = jEdit.getProperty("macro.rs.JavaFileSave.InfiniteLoop.error", "Infinite loop:");
  40.  
  41. // Check this is a new file
  42. if (buffer.isNewFile() && buffer.getPath() != null)
  43. {
  44.    // Only look further if the mode is 'java', or still the default
  45.    String buffer_mode = buffer.getMode().toString();
  46.    if (buffer_mode.equals("java") || buffer_mode.equals(jEdit.getProperty("buffer.defaultMode","")))
  47.    {
  48.       String fullpath = buffer.getPath();
  49.       VFS vfs = VFSManager.getVFSForPath(fullpath);
  50.       // Split into constituent parts
  51.       String path = vfs.getParentOfPath(fullpath);
  52.       String name = vfs.getFileName(fullpath);
  53.       
  54.       // At most, check the first 250 lines - this sounds reasonable to me
  55.       int maxLine = Math.min(buffer.getLineCount(),250);
  56.       import java.util.regex.Pattern;
  57.       import java.util.regex.Matcher;
  58.       // Build the regex - based on the offical java language spec.
  59.       Pattern regex = Pattern.compile("^\\s*(public|protected|private|static|abstract|final|native|synchronized|transient|volatile|strictfp)?\\s*(class|interface)\\s*([^ {/]*)");
  60.       boolean inComment = false;
  61.       for(int i=0;i<maxLine;i++)
  62.       {
  63.          String txt = buffer.getLineText(i);
  64.          int count = 0;
  65.             // See if this line has a the start or finish of a multiline comment
  66.          while (txt.indexOf("/*")!=-1 || txt.indexOf("*/")!=-1)
  67.             {
  68.                 // A little paranoia on my part 
  69.             count++;
  70.             if (count==1000)
  71.             {
  72.                Log.log(Log.ERROR,BeanShell.class,InfiniteLoopError+"["+txt+"]");
  73.                buffer.save(view,null,true);
  74.                return;
  75.             }
  76.                 // Look for the next starting comment if we're not in a comment
  77.             if (!inComment)
  78.             {
  79.                int commentStartIndex = txt.indexOf("/*");
  80.                if (commentStartIndex != -1)
  81.                {
  82.                   inComment = true;
  83.                   if (commentStartIndex+2 == txt.length())
  84.                      txt = "";
  85.                   else
  86.                      txt = txt.substring(commentStartIndex+2);
  87.                }
  88.             }
  89.                 // Look for the next ending comment if we are in a comment
  90.             if (inComment)
  91.             {
  92.                int commentEndIndex = txt.indexOf("*/");
  93.                if (commentEndIndex != -1)
  94.                {
  95.                   inComment = false;
  96.                   if (commentEndIndex+2 == txt.length())
  97.                      txt = "";
  98.                   else
  99.                      txt = txt.substring(commentEndIndex+2);
  100.                } else {
  101.                   continue;
  102.                }
  103.             }
  104.          }
  105.             
  106.             // We now know if the remainder of the line is in a comment or not
  107.          if (!inComment)
  108.          {
  109.             Matcher matcher = regex.matcher(txt);
  110.             if (matcher.matches())
  111.             {
  112.                // Extract the class/interface name
  113.                name = matcher.group(3)+".java";
  114.                break;
  115.             }
  116.          }
  117.       }
  118.       
  119.       // Open the VFSBrowser
  120.       String[] files = GUIUtilities.showVFSFileDialog(view,path+name,
  121.                                                 VFSBrowser.SAVE_DIALOG,false);
  122.       if(files == null)
  123.          return false;
  124.       buffer.save(view,files[0],true);
  125.       return;
  126.    }
  127. }
  128.  
  129. // This isn't a file that has been scanned, so just do a normal save
  130. buffer.save(view,null,true);
  131.  
  132. /*
  133.  
  134. Macro index data (in DocBook format)
  135.  
  136.    <listitem>
  137.       <para><filename>Java_File_Save.bsh</filename></para>
  138.       <abstract><para>Acts as a wrapper script to the Save As action. If the buffer
  139.       is a new file, it scans the first 250 lines for a Java class or interface
  140.       declaration. On finding one, it extracts the appropriate filename to be
  141.       used in the Save As dialog.</para></abstract>   
  142.       </listitem>
  143.  
  144. */
  145.